{extend name="../../../view/public/base" /}

{block name="body"}
<style>
    .notice-style{
        padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;
    }
    .notice-font{
        color: black;
    }

</style>
<div class="bw-container">
    <div class="bw-main">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card-header"><i class="fa fa-question-circle-o"></i>系统应用安装配置管理</div>
            <blockquote class="layui-elem-quote layui-text" style="border-left: 5px solid #6e56ff;"
                        id="tab_change">
                配置：用于配置应用的接入方式、支持的终端、是否为定制应用、应用描述、微信支付、支付宝支付等等。<br>
                售卖套餐：用于管理租户购买应用时可以选择购买的套餐的价格、功能等。<br>
                客户：用于便捷管理购买过应用的租户客户。<br>
                应用更新：用于应用最新版本的检测和覆盖更新，因当前还未开启云端下载覆盖更新，请已购买应用的用户把【应用压缩包】放置在【应用根目录】/public/ 目录下，再操作更新。<br>
                <span style="color: red">模板管理（应用为开放平台和小程序才会显示）：用于管理此应用的第三方平台小程序代码模板库，并且可管理为购买过此应用的租户配置小程序代码上架和升级特定的代码模板。</span><br>
            </blockquote>
            <!--列表区域-->
            <div class="layui-card">
                <table id="list" lay-filter="listFilter"></table>

                <!--目录-->
                <script type="text/html" id="dir">
                    {{ d.dir }}
                </script>
                <!--名称-->
                <script type="text/html" id="title">
                    {{ d.title }}
                </script>
                <!--描述内容-->
                <script type="text/html" id="describe">
                    {{ d.describe }}
                </script>
                <!--Logo-->
                <script type="text/html" id="logo_image">
                    {{ getImagesHtmlForIndex(d.logo_image) }}
                </script>
                <!--演示二维码-->
                <script type="text/html" id="qrcode_image">
                    {{ getImagesHtmlForIndex(d.qrcode_image) }}
                </script>
                <!--应用截图-->
                <script type="text/html" id="style_images">
                    {{ getImagesHtmlForIndex(d.style_images) }}
                </script>
                <!--销售价-->
                <script type="text/html" id="sell_price">
                    {{ d.sell_price }}
                </script>
                <!--市场价-->
                <script type="text/html" id="market_price">
                    {{ d.market_price }}
                </script>
                <!--版本-->
                <script type="text/html" id="version">
                    {{ d.version ? d.version:'请添加' }}
                </script>
                <!--小程序模板-->
                <script type="text/html" id="template_id">
                    {{ d.template_id }}
                </script>
                <!--体验天数-->
                <script type="text/html" id="expire_day">
                    {{ d.expire_day }}
                </script>
                <!--应用类型-->
                <script type="text/html" id="types">
                    {{# if (d.types.search('mini_program') != -1) { }}
                    <span class='layui-badge'>微信小程序</span>
                    {{# } }}
                    {{# if (d.types.search('app') != -1) { }}
                    <span class='layui-badge'>APP</span>
                    {{# } }}
                    {{# if (d.types.search('h5') != -1) { }}
                    <span class='layui-badge'>h5</span>
                    {{# } }}
                    {{# if (d.types.search('official') != -1) { }}
                    <span class='layui-badge'>微信公众号</span>
                    {{# } }}
                    {{# if (d.types.search('system') != -1) { }}
                    <span class='layui-badge'>系统</span>
                    {{# } }}
                    {{# if (d.types.search('tt_program') != -1) { }}
                    <span class='layui-badge'>头条小程序</span>
                    {{# } }}
                </script>
                <!--平台管理:0=关闭,1=开启-->
                <script type="text/html" id="is_manage">
                    {{# if (d.is_manage == '0') { }}
                    <span style="color: #1E9FFF;">关闭</span>
                    {{# } else if(d.is_manage == '1') { }}
                    <span style="color: #1E9FFF;">开启</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.is_manage}}</span>
                    {{# } }}
                </script>
                <!--接入方式:0=独立应用,1=开放平台-->
                <script type="text/html" id="is_openapp">
                    {{# if (d.is_openapp == '0') { }}
                    <span style="color: #1E9FFF;">独立应用</span>
                    {{# } else if(d.is_openapp == '1') { }}
                    <span style="color: #1E9FFF;">开放平台</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.is_openapp}}</span>
                    {{# } }}
                </script>
                <!--微信支付:0=关闭,1=开启-->
                <script type="text/html" id="is_wechat_pay">
                    {{# if (d.is_wechat_pay == '0') { }}
                    <span style="color: #1E9FFF;">关闭</span>
                    {{# } else if(d.is_wechat_pay == '1') { }}
                    <span style="color: #1E9FFF;">开启</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.is_wechat_pay}}</span>
                    {{# } }}
                </script>
                <!--支付宝支付:0=关闭,1=开启-->
                <script type="text/html" id="is_alipay_pay">
                    {{# if (d.is_alipay_pay == '0') { }}
                    <span style="color: #1E9FFF;">关闭</span>
                    {{# } else if(d.is_alipay_pay == '1') { }}
                    <span style="color: #1E9FFF;">开启</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.is_alipay_pay}}</span>
                    {{# } }}
                </script>
                <!--排序-->
                <script type="text/html" id="sort">
                    {{ d.sort }}
                </script>
                <!--状态:-1=未安装,0=关闭,1=开启-->
                <script type="text/html" id="status">
                    {{# if (d.status == '-1') { }}
                    <span style="color: #1E9FFF;">未安装</span>
                    {{# } else if(d.status == '0') { }}
                    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-filter="statusChange" lay-text="开启|关闭">
<!--                    <span style="color: #1E9FFF;">关闭</span>-->
                    {{# } else if(d.status == '1') { }}
                    <input type="checkbox" checked name="status" value="{{d.id}}" lay-skin="switch" lay-filter="statusChange" lay-text="开启|关闭">
<!--                    <span style="color: #1E9FFF;">开启</span>-->
                    {{# } else { }}
                    <span style="color: #1E9FFF;">{{d.status}}</span>
                    {{# } }}
                </script>
                <!--安装时间-->
                <script type="text/html" id="create_time">
                    {{ formatDate(d.create_time) }}
                </script>
                <!--更新时间-->
                <script type="text/html" id="update_time">
                    {{ formatDate(d.update_time) }}
                </script>

                <!--状态开关-->
                <script type="text/html" id="listBar">
                    <!--应用未安装-->
                    {{# if (d.status == '-1') { }}
                    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="install">安装</a>
                    <!--应用已安装 禁用中-->
                    {{# } else if(d.status == '0') { }}
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">配置</a>
                        {{# if (d.is_openapp == '1' && (d.types.indexOf("mini_program") != -1)) { }}
                         <a class="layui-btn layui-btn-xs layui-btn-green" lay-event="template" title="第三方平台小程序代码模板管理">模板管理</a>
                        {{# } }}
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="module" title="租户应用购买套餐">售卖套餐</a>
                    <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="uninstall">卸载</a>
                    <!--应用已安装 启用中-->
                    {{# } else if(d.status == '1') { }}
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">配置</a>
                        {{# if (d.is_openapp == '1' && (d.types.indexOf("mini_program") != -1)) { }}
                        <a class="layui-btn layui-btn-xs layui-btn-green" lay-event="template" title="第三方平台小程序代码模板管理">模板管理</a>
                        {{# } }}
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="module" title="租户应用购买套餐">售卖套餐</a>
                    <!--<a class="layui-btn layui-btn-xs layui-btn-primary  " lay-event="renew" title="租户应用续费套餐">续费套餐</a>-->
                    <!--查看客户购买该应用的列表-->
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="member">客户</a>
                    {{# } else { }}
                    {{# } }}
                    <!--检查更新-->
                    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="update">检查更新</a>
                </script>
                <!--定制应用-->
                <script type="text/html" id="is_diy">
                    {{# if (d.is_diy == '0') { }}
                    <span style="color: #1E9FFF;">否</span>
                    {{# } else if(d.is_diy == '1') { }}
                    <span style="color: #1E9FFF;">是</span>
                    {{# } else { }}
                    <span style="color: #1E9FFF;">-</span>
                    {{# } }}
                </script>
            </div>
        </div>
    </div>
    </div>
</div>

<script id="update_detail_template" type="text/html">
    <div class = 'notice-style'>

        <blockquote class="layui-elem-quote notice-font" >
            发现新版本 ！！<br>
           {{d.info}}<br>
            发布时间：{{d.update_time_date}}<span class="layui-badge">版本号：{{d.version}}</span>
        </blockquote>

        <br>
        <span class="layui-badge">更新说明</span>
        <br>
        {{#  layui.each(d.add, function(index, item){ }}
        <br><span class="layui-badge layui-bg-green">新增</span> - {{item.msg}}
        {{#  }); }}
        <br>
        {{#  layui.each(d.optimize, function(index, item){ }}
        <br><span class="layui-badge layui-bg-blue">优化</span> - {{item.msg}}
        {{#  }); }}
        <br>
        {{#  layui.each(d.repair, function(index, item){ }}
        <br><span class="layui-badge layui-bg-orange">修复</span> - {{item.msg}}
        {{#  }); }}
    </div>
</script>
{/block}

{block name="js"}
<script>
    var form, table;
    var laytpl;
    layui.use(['form', 'table', 'bwajax','laytpl'], function () {
        form = layui.form;
        table = layui.table;
        laytpl = layui.laytpl;
        //执行列表
        table.render({
            elem: '#list',
            url: "{:Url('/manage/admin/Miniapp/index')}",
            response: {
                statusCode: 200,  //规定成功的状态码，默认：0
            },
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.data.total, //解析数据长度
                    "data": res.data.data.list //解析数据列表
                };
            },
            // toolbar: '#listToolbar',
            cols: [[
                // {type:'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', align: 'center', width: 80, fixed: 'left', hide:true},
                {field: 'logo_image', title: 'Logo', align: 'center', width: 80, templet: '#logo_image'},
                {field: 'title', title: '名称', align: 'center', width: 180, templet: '#title'},
                {field: 'types', title: '应用类型', align: 'center', minWidth: 200, templet: '#types'},
                {field: 'is_openapp', title: '接入方式', align: 'center', width: 100, templet: '#is_openapp'},
                // {field: 'sell_price', title: '销售价', align: 'center', width: 80, templet: '#sell_price'},
                {field: 'dir', title: '目录', align: 'center', width: 150, templet: '#dir'},
                {field: 'version', title: '应用版本', align: 'center', width: 150, templet: '#version'},
                {field: 'status', title: '定制应用', align: 'center', width: 100, templet: '#is_diy'},
                {field: 'status', title: '售卖状态', align: 'center', width: 100, templet: '#status'},
                // {field: 'describe', title: '描述内容', align: 'center', minWidth: 150, templet: '#describe', hide:true},
                // {field: 'qrcode_image', title: '演示二维码', align: 'center', width: 60, templet: '#qrcode_image', hide:true},
                // {field: 'style_images', title: '应用截图', align: 'center', width: 150, templet: '#style_images', hide:true},
                // {field: 'market_price', title: '市场价', align: 'center', width: 80, templet: '#market_price', hide:true},
                // {field: 'template_id', title: '小程序模板', align: 'center', width: 80, templet: '#template_id', hide:true},
                // {field: 'expire_day', title: '体验天数', align: 'center', width: 80, templet: '#expire_day', hide:true},
                // {field: 'is_manage', title: '平台管理', align: 'center', width: 100, templet: '#is_manage', hide:true},
                // {field: 'is_wechat_pay', title: '微信支付', align: 'center', width: 100, templet: '#is_wechat_pay', hide:true},
                // {field: 'is_alipay_pay', title: '支付宝支付', align: 'center', width: 100, templet: '#is_alipay_pay', hide:true},
                // {field: 'sort', title: '排序', align: 'center', width: 80, templet: '#sort', hide:true},
                // {field: 'create_time', title: '安装时间', align: 'center', width: 160, templet: '#create_time', hide:true},
                // {field: 'update_time', title: '更新时间', align: 'center', width: 160, templet: '#update_time', hide:true},
                {title: '操作', toolbar: '#listBar', align: 'left', fixed: "right", width: 400}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            // page: true
        });
        //监听表格头部工具栏事件
        table.on('toolbar(listFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'refresh':
                    reload();
                    break;
                case 'add':
                    layer.open({
                        title: '新增',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('/manage/admin/Miniapp/add')}",
                    });
                    break;
                case 'del':
                    var checkStatus = table.checkStatus('list');
                    var data = checkStatus.data;
                    var idsArr = [];
                    data.forEach(function(value) {
                        idsArr.push(value.id);
                    });
                    ids = idsArr.join(',');
                    if(!ids) layer.msg('没有选中行', {icon: 2});
                    else del(ids);
                    break;
            }
        });

        //监听工具条
        table.on('tool(listFilter)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            switch (layEvent) {
                case 'edit':
                    layer.open({
                        title: "【" + data.title + "】",
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('/manage/admin/Miniapp/edit')}?id=" + data.id,
                    });
                    break;
                case 'template':
                    template(obj.data.id);
                    break;
                case 'install'://安装
                    install(obj.data.dir);
                    break;
                case 'member'://安装
                    member(obj.data.id);
                    break;
                case 'uninstall':
                    uninstall(obj.data.dir);
                    break;
                case 'module':
                    layer.open({
                        title: "【" + data.title + "】",
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['100%', '90%'],
                        content: "{:Url('/manage/admin/miniapp/module/index')}?miniapp_id=" + data.id,
                    });
                    break;
                case 'renew':
                    layer.open({
                        title: "【" + data.title + "】续费套餐",
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['100%', '90%'],
                        content: "{:Url('/manage/admin/miniapp/term/index')}?miniapp_id=" + data.id,
                    });
                    break;
                case 'update'://检查更新
                    appUpdate.check(obj.data.dir);
                    break;
            }
        });

        form.on('submit(searchBtn)', function (data) {
            reload(1);
            return false;
        })

        //监听switch开关
        form.on('switch(statusChange)', function(obj){
            var state = this.checked ? '1' : '0';

            var id = obj.value;
            bwajax.post("{:Url('/manage/admin/Miniapp/edit')}?id="+id, {id:id,status:state})
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('修改成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                        reload();
                    }
                })
        });
    });

    //表格重载
    function reload(page) {
        var where = getWhere();//获取搜索条件
        var page_now = $(".layui-laypage-em").next().html(); //当前页码值
        var page_to = page ? page : page_now;

        table.reload('list', {
            where: where, //设定异步数据接口的额外参数
            // page: {curr: page_to}//设置页数
        });
    }

    //安装
    function install(dir) {
        layer.confirm('确认安装?', function (index) {
            //加载loading
            var loading_index =  layer.msg('应用安装中，安装期间请勿关闭或刷新页面...', {
                icon: 16
                ,shade: 0.6
            });
            //ajax调用后台接口
            bwajax.post("{:Url('/manage/admin/Miniapp/install')}?dir=" + dir)
                .then(function (response) {
                    //关闭loading
                    layer.close(loading_index);
                    if (response.data.data.errcode === 0) {
                        layer.msg('安装成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
            layer.close(index);
        });
    }

    //安装
    function uninstall(dir) {
        layer.confirm('确认卸载?', function (index) {
            //加载loading
            var loading_index =  layer.msg('应用卸载中，卸载期间请勿关闭或刷新页面...', {
                icon: 16
                ,shade: 0.6
            });
            //ajax调用后台接口
            bwajax.post("{:Url('/manage/admin/Miniapp/uninstall')}?dir=" + dir)
                .then(function (response) {
                    //关闭loading
                    layer.close(loading_index);
                    if (response.data.data.errcode === 0) {
                        layer.msg('卸载成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
            layer.close(index);
        });
    }

    //查看客户购买该应用的列表
    function member(miniapp_id) {
        layer.open({
            title: '客户应用',
            type: 2,
            shade: 0.2,
            maxmin: true,
            area: ['50%', '90%'],
            content: "/manage/admin/MemberMiniapp/index?miniapp_id=" + miniapp_id,
        });
    }
    //第三方平台模板管理
    function template(miniapp_id) {
        layer.open({
            title: '小程序代码模板库管理【用于为每个租户小程序程序升级及上架】',
            type: 2,
            shade: 0.2,
            maxmin: true,
            area: ['98%', '90%'],
            content: "/manage/admin/template?miniapp_id=" + miniapp_id,
        });
    }

    //应用更新对象
    var appUpdate = {
        param:{
            dir:null, //目标应用
            check:0, //是否是版本检测
        },
        data:{
            url:"{:Url('/manage/admin/Miniapp/update')}",  //接口url
        },
        check:function (dir) {
            this.param.dir = dir;
            this.param.check = 1;
           //调用检测
            this.check_update();
        },
        check_update:function () {
            var _this =this;
            bwajax.post(this.get_url(),this.param)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        _this.check_callback(response.data.data.data);
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
        },
        check_callback:function (data) {
            var _this = this;
            var getTpl = update_detail_template.innerHTML;
            laytpl(getTpl).render(data, function(html){
                if(data.download_url){
                    var btn = ['覆盖更新', '下载最新版本'];
                }else{
                    var btn = ['覆盖更新'];
                }
                //示范一个公告层
                layer.open({
                    type: 1
                    ,title: false //不显示标题栏
                    ,closeBtn: true
                    ,area: '450px;'
                    ,shade: 0.8
                    ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                    ,btn: btn
                    ,btnAlign: 'c'
                    ,moveType: 1 //拖拽模式，0或者1
                    ,content: html
                    ,success: function(layero){
                        //打开弹窗回调
                    }
                    ,yes: function(index, layero){
                        layer.close(index);
                        //覆盖更新
                        _this.update(data);
                    }
                    ,btn2: function(index, layero){
                        //下载最新版本
                        var btn = layero.find('.layui-layer-btn');
                        btn.find('.layui-layer-btn1').attr({
                            href: data.download_url
                        });
                    }
                });
            });
        },
        update:function (data) { //调用更新
            console.log(data); //local_package
            var msg = '确认覆盖更新么？确认后将会下载【远程最新应用】进行更新，强烈建议更新前先备份下当前应用目录和数据库！更新后将会覆盖当前应用目录并更新菜单节点相关sql和相关应用表结构！';
            var btn_msg = '确认下载更新';
            if(data.local_package){
                msg = '检测到您本地存在更新包【'+data.info+'】，版本号为【'+data.version+'】,将用该更新包进行更新！强烈建议更新前先备份下当前应用目录和数据库！更新后将会覆盖当前应用目录并更新菜单节点相关sql和相关应用表结构！';
                btn_msg = '确认本地更新包更新';
            }
            var _this = this;
            layer.confirm(msg, {
                btn: [btn_msg, '取消'] //可以无限个按钮
            }, function(index, layero){
                _this.cover(data,index); //覆盖更新
            }, function(index){
                layer.close(index); //取消
            });
        },
        cover:function (data,index) {
          var load = layer.msg('覆盖更新中，请勿刷新当前页', {
                icon: 16
                ,shade: 0.01
            });
            this.param.check = 0;
            var _this = this;
            bwajax.post(this.get_url(),this.param)
                .then(function (response) {
                    layer.close(load); //关闭加载
                    if (response.data.data.errcode === 0) {
                        _this.cover_suceess(response.data.data.data,index);
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
             });
        },
        cover_suceess:function (data,index) { //执行成功回调
            layer.close(index); //关闭弹层
            layer.msg('覆盖更新成功', {icon: 1});
            reload();
        },
        get_url:function () {
            var url_param = $.param(this.param);
            var url = this.data.url +"?" + url_param;
            return url;
        }
    }

</script>
{/block}